﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KNXObjects.Types;

namespace KNXObjects
{
    public class BlindObject : GenericObjectBase<StepDirType>
    {
        #region ObjectBase Implementations

        public override string DataPointEISName
        {
            get { return string.Empty; }
        }

        public override string DataPointID
        {
            get { return "3.008"; }
        }

        public override string ToString()
        {
            if (Value.StepCode == 0)
                return "stop";

            string ret = "{0}:0{1}";
            return string.Format(ret, Value.Direction == 1 ? "close" : "open", Value.StepCode);
        }

        public override void SetValue(string value)
        {
            string[] splittedValues = value.Split(':');
            if (splittedValues.Length > 0)
            {
                Value.StepCode = 1;
                string dir = splittedValues[0].ToLower();
                if (splittedValues.Length == 2)
                    Value.StepCode = Convert.ToInt32(splittedValues[1]);

                if (dir == "stop")
                {
                    Value.StepCode = 0;
                    Value.Direction = 0;
                }
                else if (dir == "close")
                    Value.Direction = 1;
                else if (dir == "open")
                    Value.Direction = 0;
                else
                    throw new ApplicationException(string.Format("BlindsObjectValue: Bad value: '{0}'", value));

            }
        }        

        #endregion

        #region ctor

        public BlindObject(string id, string address, string name, string flags)
            : base(id, address, name, flags)
        {
        }
        public BlindObject(int dir, int step)
        {
            Value.Direction = dir;
            Value.StepCode = step;
        }

        public BlindObject(string value)
        {
            SetValue(value);
        }

        #endregion

    }
}
